package com.ruoyi.platform.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.platform.domain.DishAudit;
import com.ruoyi.platform.service.IDishAuditService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;

/**
 * 商家菜品上架审核Controller
 *
 * @author ruoyi
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/platform/dish_audit")
public class DishAuditController extends BaseController
{
    @Autowired
    private IDishAuditService dishAuditService;

    /**
     * 查询商家菜品上架审核列表
     */
    @PreAuthorize("@ss.hasPermi('platform:dish_audit:list')")
    @GetMapping("/list")
    public TableDataInfo list(DishAudit dishAudit)
    {
        startPage();
        List<DishAudit> list = dishAuditService.selectDishAuditList(dishAudit);
        return getDataTable(list);
    }

    /**
     * 导出商家菜品上架审核列表
     */
    @PreAuthorize("@ss.hasPermi('platform:dish_audit:export')")
    @Log(title = "商家菜品上架审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DishAudit dishAudit)
    {
        List<DishAudit> list = dishAuditService.selectDishAuditList(dishAudit);
        ExcelUtil<DishAudit> util = new ExcelUtil<DishAudit>(DishAudit.class);
        util.exportExcel(response, list, "商家菜品上架审核数据");
    }

    /**
     * 获取商家菜品上架审核详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:dish_audit:query')")
    @GetMapping(value = "/{auditId}")
    public AjaxResult getInfo(@PathVariable("auditId") Long auditId)
    {
        return success(dishAuditService.selectDishAuditByAuditId(auditId));
    }

    /**
     * 新增商家菜品上架审核
     */
    @PreAuthorize("@ss.hasPermi('platform:dish_audit:add')")
    @Log(title = "商家菜品上架审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DishAudit dishAudit)
    {
        return toAjax(dishAuditService.insertDishAudit(dishAudit));
    }

    /**
     * 修改商家菜品上架审核
     */
    @PreAuthorize("@ss.hasPermi('platform:dish_audit:edit')")
    @Log(title = "商家菜品上架审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DishAudit dishAudit)
    {
        return toAjax(dishAuditService.updateDishAudit(dishAudit));
    }

    /**
     * 删除商家菜品上架审核
     */
    @PreAuthorize("@ss.hasPermi('platform:dish_audit:remove')")
    @Log(title = "商家菜品上架审核", businessType = BusinessType.DELETE)
	@DeleteMapping("/{auditIds}")
    public AjaxResult remove(@PathVariable Long[] auditIds)
    {
        return toAjax(dishAuditService.deleteDishAuditByAuditIds(auditIds));
    }

    /**
     * 审核通过
     */
    @PreAuthorize("@ss.hasPermi('platform:dish_audit:approve')")
    @Log(title = "菜品审核通过", businessType = BusinessType.UPDATE)
    @PutMapping("/approve/{auditId}")
    public AjaxResult approve(@PathVariable Long auditId, @RequestBody Map<String, String> params)
    {
        String auditRemark = params.get("auditRemark");
        Long auditorId = SecurityUtils.getUserId();
        String auditorName = SecurityUtils.getUsername();

        return toAjax(dishAuditService.approveDish(auditId, auditorId, auditorName, auditRemark));
    }

    /**
     * 审核拒绝
     */
    @PreAuthorize("@ss.hasPermi('platform:dish_audit:reject')")
    @Log(title = "菜品审核拒绝", businessType = BusinessType.UPDATE)
    @PutMapping("/reject/{auditId}")
    public AjaxResult reject(@PathVariable Long auditId, @RequestBody Map<String, String> params)
    {
        String rejectReason = params.get("rejectReason");
        Long auditorId = SecurityUtils.getUserId();
        String auditorName = SecurityUtils.getUsername();

        return toAjax(dishAuditService.rejectDish(auditId, auditorId, auditorName, rejectReason));
    }

    /**
     * 批量审核通过
     */
    @PreAuthorize("@ss.hasPermi('platform:dish_audit:approve')")
    @Log(title = "批量菜品审核通过", businessType = BusinessType.UPDATE)
    @PutMapping("/batchApprove")
    public AjaxResult batchApprove(@RequestBody Map<String, Object> params)
    {
        @SuppressWarnings("unchecked")
        List<Long> auditIdList = (List<Long>) params.get("auditIds");
        Long[] auditIds = auditIdList.toArray(new Long[0]);
        String auditRemark = (String) params.get("auditRemark");
        Long auditorId = SecurityUtils.getUserId();
        String auditorName = SecurityUtils.getUsername();

        return toAjax(dishAuditService.batchApproveDish(auditIds, auditorId, auditorName, auditRemark));
    }

    /**
     * 批量审核拒绝
     */
    @PreAuthorize("@ss.hasPermi('platform:dish_audit:reject')")
    @Log(title = "批量菜品审核拒绝", businessType = BusinessType.UPDATE)
    @PutMapping("/batchReject")
    public AjaxResult batchReject(@RequestBody Map<String, Object> params)
    {
        @SuppressWarnings("unchecked")
        List<Long> auditIdList = (List<Long>) params.get("auditIds");
        Long[] auditIds = auditIdList.toArray(new Long[0]);
        String rejectReason = (String) params.get("rejectReason");
        Long auditorId = SecurityUtils.getUserId();
        String auditorName = SecurityUtils.getUsername();

        return toAjax(dishAuditService.batchRejectDish(auditIds, auditorId, auditorName, rejectReason));
    }

    /**
     * 根据商家ID查询待审核菜品列表
     */
    @PreAuthorize("@ss.hasPermi('platform:dish_audit:list')")
    @GetMapping("/pending/{merchantId}")
    public AjaxResult getPendingDishByMerchantId(@PathVariable Long merchantId)
    {
        List<DishAudit> list = dishAuditService.selectPendingDishByMerchantId(merchantId);
        return success(list);
    }

    /**
     * 获取审核统计数据
     */
    @PreAuthorize("@ss.hasPermi('platform:dish_audit:list')")
    @GetMapping("/statistics")
    public AjaxResult getStatistics()
    {
        Map<String, Object> statistics = dishAuditService.getAuditStatistics();
        return success(statistics);
    }
}
